<template>
  <a-layout style="min-height: 100vh">
    <a-layout-sider collapsible v-model:collapsed="collapsed">
      <div class="logo">
        <span class="logo-title">就医助手</span>
      </div>
      <a-menu theme="dark" mode="inline" :openKeys="openKeys" :selectedKeys="selectedKeys" @openChange="onOpenChange" @click="onMenuClick">
        <a-sub-menu key="assistant" title="就医助手">
          <a-menu-item key="qa">智能问答</a-menu-item>
          <a-menu-item key="record">就诊记录</a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header class="header">
        <div class="user-info">
          <a-avatar style="background-color: #87d068; margin-right: 8px;">
            {{ user.username.charAt(0).toUpperCase() }}
          </a-avatar>
          <span>{{ user.username }}</span>
          <span style="margin-left: 16px; color: #888;">{{ user.email }}</span>
        </div>
      </a-layout-header>
      <a-layout-content style="margin:24px 16px 0;">
        <div class="content-box">
          <router-view />
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const collapsed = ref(false)
const openKeys = ref(['assistant'])
const selectedKeys = ref(['qa'])
const router = useRouter()
const route = useRoute()

const user = JSON.parse(localStorage.getItem('user') || '{}')

// 根据当前路由设置选中菜单
watch(
  () => route.path,
  (val) => {
    if (val.endsWith('/qa')) selectedKeys.value = ['qa']
    else if (val.endsWith('/record')) selectedKeys.value = ['record']
    else selectedKeys.value = ['qa']
  },
  { immediate: true }
)

function onOpenChange(keys) {
  openKeys.value = keys
}
function onMenuClick({ key }) {
  selectedKeys.value = [key]
  router.push(`/medical/${key}`)
}
</script>

<script>
export default {
  name: 'MedicalPage'
}
</script>

<style scoped>
.logo {
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #fff;
  font-weight: bold;
  margin: 16px 0;
}
.logo-title {
  letter-spacing: 2px;
}
.header {
  background: #fff;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 64px;
  box-shadow: 0 2px 8px #f0f1f2;
}
.user-info {
  display: flex;
  align-items: center;
  font-size: 16px;
}
.content-box {
  background: #fff;
  min-height: 360px;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 8px #f0f1f2;
}
</style>
